package org.jfbmonitor.view.attachment;

import java.awt.Color;
import java.awt.Component;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class AttachmentCellRenderer extends DefaultTableCellRenderer {
	private static final long serialVersionUID = 1L;

	public AttachmentCellRenderer() {
		super();
	}

	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		JLabel label = (JLabel) super.getTableCellRendererComponent(table,
				value, isSelected, hasFocus, row, column);

		value = format(value);
		if (column == 0) {
			label.setHorizontalAlignment(JLabel.CENTER);
		} else {
			label.setHorizontalAlignment(JLabel.LEFT);
		}
		if (isSelected) {
			label.setForeground(Color.WHITE);
			Color color = new Color(10, 36, 100);
			label.setBackground(color);
		} else {
			label.setForeground(Color.BLACK);

			if (row % 2 != 0) {
				Color yellow = new Color(255, 255, 223);
				label.setBackground(yellow);
			} else {
				label.setBackground(Color.WHITE);
			}

		}
		return label;
	}

	private Object format(Object value) {
		if (value instanceof Date) {
			Date date = (Date) value;
			DateFormat df = DateFormat.getDateTimeInstance();
			System.out.println(df.format(date));
			return df.format(date);			
		} else {
			return value;
		}
	}

}
